<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item label="研究方向码">
              <a-input v-model="queryParam.yjfxm"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="名称">
              <a-input v-model="queryParam.yjfxmc"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="12">
              <a-form-item label="院系">
                <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch :filterOption="filterOption" v-model="queryParam.ssyxm" @change="yxChange">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="所属专业">
                <a-select :allowClear="true" showSearch :filterOption="filterOption" v-model="queryParam.sszym">
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="学位层次">
                <a-select :allowClear="true" v-model="queryParam.xwccm">
                  <a-select-option v-for="i in selectMenu.xwcc" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="是否全日制">
                <a-select :allowClear="true" v-model="queryParam.isqrz">
                  <a-select-option v-for="i in selectMenu.isqrz" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="招生年度">
                <a-select :allowClear="true" v-model="queryParam.zsxn">
                  <a-select-option v-for="i in selectMenu.zsxn" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="16" :sm="24">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary"  @click="$refs.table.refresh(true)">查询</a-button>
              <a-button type="primary" v-hasPermission="'yjfx:add'" @click="openAdd">添加</a-button>
              <a-button type="primary" @click="exportEcl">导出</a-button>
              <a-button type="primary" v-hasPermission="'yjfx:delete'" @click="batchDelete">删除</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <research-edit
      ref="researchEdit"
      :visible.sync="visible"
      :editId.sync="editId"
      :selectMenu.sync="selectMenu"
      @cancel="visible = false"
      @create="handleCreate"
    />
    <s-table
      v-if="initDone"
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      :scroll="{ x: 1400 }"
      >

      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
      <span slot="yjfxm" slot-scope="text, record">
        {{ record.sszym }}{{ record.zsxn }}{{ text }}
      </span>
      <a slot="operation" v-hasPermission="'yjfx:update'" slot-scope="a,b" @click="openEdit(b)">修改</a>
    </s-table>
  </a-card>
</template>

<script>
import { STable } from '~'
import researchEdit from './researchEdit'

export default {
  name: 'TableList',
  components: {
    STable,
    researchEdit
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: true,
      visible: false,
      editId: null,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '序号',
          width: 80,
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '研究方向码',
          dataIndex: 'yjfxm',
          // scopedSlots: { customRender: 'yjfxm' },
        },
        {
          title: '研究方向名称',
          dataIndex: 'yjfxmc'
        },
        {
          title: '所属院系',
          dataIndex: 'ssyxmc'
        },
        {
          title: '所属专业',
          dataIndex: 'sszymc'
        },
        {
          title: '学位层次',
          width: 150,
          dataIndex: 'xwccmc'
        },
        {
          title: '是否全日制',
          width: 150,
          dataIndex: 'isqrzmc'
        },
        {
          title: '招生学年',
          width: 120,
          dataIndex: 'zsxnmc'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: 80,
          scopedSlots: { customRender: 'operation' },
          fixed: 'right'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        console.log('loadData.parameter', parameter)
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.base.researchList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true, // 是否显示alert，已选中
      confirmLoading: false,
      selectMenu: {
        yx: [], // 院系
        zy: [], // 所属专业
        xwcc: [], // 学位层次
        isqrz: [], // 是否全日制
        zsxn: [] // 招生学年
      },
      initDone: false
    }
  },
  created () {
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$api.base.dictionaryData('TYPE0119'),
        this.$api.base.dictionaryData('TYPE0441'),
        this.$api.base.dictionaryData('TYPE0387')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data,
          xwcc: res[1].data,
          isqrz: res[2].data,
          zsxn: res[3].data
        }
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'ssyxm', this.selectMenu.yx[0].dwh)
          this.yxChange(this.selectMenu.yx[0].dwh)
        }
        this.initDone = true
      })
    },
    handleCreate () {
      const form = this.$refs.researchEdit.form
      form.validateFields((err, values) => {
        if (err) {
          return
        }
        let baseApi = this.editId ? 'researchUpdate' : 'researchAdd'
        values.yjfxm = this.$refs.researchEdit.researchCode[0] + this.$refs.researchEdit.researchCode[1] + values.yjfxm
        this.$api.base[baseApi](Object.assign({id: this.editId}, values)).then(res => {
          this.$message.success(res.msg)
          this.$refs.table.refresh(false)
        })
        this.visible = false
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    openAdd () {
      this.editId = null
      this.visible = true
      this.$refs.researchEdit.zyList = []
      this.$refs.researchEdit.researchCode = new Array(3)
      this.$refs.researchEdit.form.resetFields()
    },
    openEdit (research) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.researchEdit.form.resetFields()
        this.$refs.researchEdit.form.setFieldsValue({...research, yjfxm: research.yjfxm.replace(research.sszym, '').replace(research.zsxn, '')})
        this.$refs.researchEdit.yxChange(research.ssyxm, true)
        this.$refs.researchEdit.researchCode[0] = research.sszym
        this.$refs.researchEdit.researchCode[1] = research.zsxn
        this.editId = research.id
      })
    },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的研究方向信息')
        return
      }
      this.$confirm({
        title: '确定删除所选中的研究方向信息?',
        content: '当您点击确定按钮后，这些记录将会被彻底删除',
        centered: true,
        onOk: () => {
          let campusIds = []
          this.selectedRows.forEach(campus => {
            campusIds.push(campus.id)
          })
          this.$api.base.researchDelete(campusIds.join(',')).then(res => {
            this.$message.success(res.msg)
            this.selectedRowKeys = []
            this.selectedRows = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => {
        }
      })
    },
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      this.$api.base.majorAll({lsdwh: code}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
    },
    exportEcl(){
      this.$export('base/yjfx/exportList',this.queryParam)
    }
  }
}
</script>
